<?php
session_start();
$dir = rtrim($_SESSION['dir'],"/")."/";
if (empty($_SESSION['dir']) || !is_dir($_SESSION['dir']))
{
	echo "指定系统目录不在";
	exit;
}
$_SESSION['ModuleName'] = $_POST['ModuleName'];
$_SESSION['ModuleUrlName'] = $_POST['ModuleUrlName'];
$name = explode('_', $_POST['ModuleUrlName']);
for($i = 0, $len = count($name); $i < $len; $i++ ) {
    if ($i == 0) {
        $name[$i] = strtolower($name[$i]);
    } else {
    	$name[$i] = ucfirst(strtolower($name[$i]));
    }
}

$_SESSION['ModuleVarName'] = implode('', $name);
$_SESSION['ModuleFileName'] = ucfirst($_SESSION['ModuleVarName']);
$_SESSION['TableName'] = $_POST['TableName'];
$_SESSION['PageNum'] = $_POST['PageNum'];

include_once("./class/sql.inc.php");
$mysql=new sql_db($_SESSION['host'],$_SESSION['user'],$_SESSION['pwd'],$_SESSION['dbname']);
$sql = "describe `".$_SESSION['TableName']."`";
$rs = $mysql->re_datas($sql);
$str = "";
$i = 0;
$type_option = '<option value="">不检查</option><option value="mobile">手机</option><option value="number">数字</option><option value="email">邮箱</option><option value="year">年份</option><option value="month">月份</option><option value="day">日期</option><option value="img">有效图片地址</option><option value="url">有效URL</option>';
$has_auto_increment = false;
foreach ($rs as $v)
{
    $primary = $v['Extra'] == 'auto_increment' ? ' checked' : '';
    $has_auto_increment |= $v['Extra'] == 'auto_increment' ? 1 : 0;
	$add_checked = $i!=0 ? "checked" : "";
	$str .= "<tr align=center><td>".$v['Field']."</td>
	<td><input type='text' name='name[".$i."]' value='".strtoupper($v['Field'])."'></td>";
    if ($v['Extra'] == 'auto_increment') {
        $str .= str_replace("type='checkbox'", "type='checkbox' checked", "<td><input type='checkbox' name='empty[".$i."]' value='1'></td>");
    } else {
        $str .= "<td><input type='checkbox' name='empty[".$i."]' value='1'></td>";
    }
    $str .= "<td><select name='check[".$i."]'>".$type_option."</select></td>";
	$str .= "<td><input type='checkbox' name='show_field[".$i."]' value='".$v['Field']."'></td>
	<td><input type='checkbox' name='add_field[".$i."]' value='".$v['Field']."' ".$add_checked."></td>
	<td><input type='checkbox' name='search_field[".$i."]' value='".$v['Field']."'></td>
	<td><input type='checkbox' name='primary[".$i."]' value='".$v['Field']."'{$primary}></td>";
    $str .=  empty($_POST['logic_delete']) ? '' : "<td><input type='radio' name='logic_delete' value='".$v['Field']."'></td>";
    $str .= "</tr>
    <input type='hidden' name='type_field[".$i."]' value='" . $v['Type'] . "' id=''/>
    <input type='hidden' name='field[".$i."]' value='" . $v['Field'] . "' id=''/>";
	$i++;
}
$_SESSION['has_auto_increment'] = $has_auto_increment;
$title = empty($_POST['logic_delete']) ? '' : '<td>逻辑删除</td>';
?>
<html>
<head>
<title>模板构架</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>
body {text-align:center;padding-top:50px;background-color:#F7E8AC;}
table {font-size:12px; line-height:20px;border-color:#FF9900; border-style:groove}
th {background-color:#FF9900; font-size:14px; font-weight:bold;}
td {padding-left:5px; border-style:inherit; border:1px dashed #FF9900;}
</style>
</head>
<body>
<form action="./finally.php" method="POST">
<table align=center border=1>
<tr><th colspan="10">设置字段</th></tr>
<tr align=center><td>字段</td><td>填写中文名</td><td>添加时传入允许为空</td><td>类型检测</td><td>是否显示在列表</td><td>是否生成添加form</td><td>是否生成搜索</td><td>主键</td><?php echo $title;?></tr>
<?php echo $str;?>
<tr><td colspan=8 align=center><input type="submit" value="下一步"></td></tr>
</table>
</form>
</body>
</html>